<template>
	<form class='loginView' @submit="register">
		<view class="input-view">
			<view class="label-view">
				<text class="label">昵称</text>
			</view>
			<input v-model="User.username" class="input" type="text" placeholder="请输入用户名"  />
		</view>
		<view class="input-view">
			<view class="label-view">
				<text class="label">手机号</text>
			</view>
			<input v-model="User.phone" class="input" type="text" placeholder="请输入手机号"  />
		</view>
		<view class="input-view">
			<view class="label-view">
				<text class="label">密码</text>
			</view>
			<input v-model="User.password" class="input" type="password" placeholder="请输入密码"  />
		</view>
		<view class="input-view">
			<view class="label-view">
				<text class="label">确认密码</text>
			</view>
			<input v-model="User.rePassword" class="input" type="password" placeholder="请输入密码"  />
		</view>
		<view class="button-view">
			<button  formType="submit"  type="default" class="login" hover-class="hover">注册</button>
		</view>
	</form>
</template>

<script>
	import reqests from "../../utils/requests.js";
	import {registerApi} from "../../http/login.js"
	
	export default {
		data() {
			return {
				User:{
					username:'',
					phone:'',
					password:'',
					rePassword:''
				},
				// User:{
				// 	username:'1234',
				// 	phone:'13298712976',
				// 	password:'12345678',
				// 	rePassword:'12345678'
				// }
			};
		},
		methods: {
			 checkForm(){
					// 校验名称
					let nameTexts = /^[a-zA-Z0-9_\u4e00-\u9fa5]{3,20}$/
					if(!nameTexts.test(this.User.username)){
						uni.showToast({
							title:'名称格式错误',
							icon:'none',
							duration:2000
						})
						return false;
					}
					// 校验手机号
					let phoneTest = /^1[3-9]\d{9}$/
					if(!phoneTest.test(this.User.phone)){
						uni.showToast({
							title:'手机号格式错误',
							icon:'none',
							duration:2000
						})
						return false;
					}
					// 校验密码
					let pasTest = /^[a-zA-Z0-9]{8,16}$/
					if(!pasTest.test(this.User.password)){
						uni.showToast({
							title:'密码格式错误',
							icon:'none',
							duration:2000
						})
						return false;
					}
					if(this.User.password!=this.User.rePassword){
						uni.showToast({
							title:'输入密码不一致',
							icon:'none',
							duration:2000
						})
						return false;
					}
					return true;
			},
			
			
			async register() {
				console.log(this.User)
				 if(!this.checkForm()){
					 return;
				 }
				 const res = await registerApi(this.User);
				 if(res.code == 1){
					 uni.navigateBack()
					 // uni.redirectTo({
					 // 	url:"/pages/login/login"
					 // })
				 }else{
					 uni.showToast({
					 	title:'注册失败  请检查网络信息',
					 	icon:'none',
					 	duration:2000
					 })
				 }
			}
		}
	}
</script>

<style>
	
</style>
